package educrypt.gui.components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.ConsoleBigView;
import educrypt.gui.view.EducryptFileChooserView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class ConsoleBigDialog extends EducryptDialog implements ConsoleBigView {

	private static final long serialVersionUID = 8200696352381662957L;

	private final JPanel contentPanel = new JPanel();
	private JScrollPane scrollPane;
	private JTextPane txtLog;
	private JButton btnSaveLog, btnClearLog;
	private EducryptFileChooser saveFileDialog;

	public ConsoleBigDialog(Window owner) {
		super(owner);
		this.setModal(false);

		setTitle(Messages.getString("guiConsole"));

		this.setSize(500, 650);
		this.setPreferredSize(contentPanel.getSize());

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblConsole = new JLabel(Messages.getString("guiConsole"));
		lblConsole.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblConsole.setBounds(20, 8, 378, 24);
		contentPanel.add(lblConsole);

		scrollPane = new JScrollPane((Component) null);
		scrollPane.setBounds(25, 40, 420, 590);
		contentPanel.add(scrollPane);

		txtLog = new JTextPane();
		txtLog.setEditable(false);
		scrollPane.setViewportView(txtLog);

		ImageIcon saveIcon = new ImageIcon(
				EduCryptResources.getImageURL("save24.png"));

		btnSaveLog = new JButton(saveIcon);
		contentPanel.add(btnSaveLog);
		btnSaveLog.setBounds(450, 602, 28, 28);

		ImageIcon clearLogIcon = new ImageIcon(
				EduCryptResources.getImageURL("remove24color.png"));

		btnClearLog = new JButton(clearLogIcon);
		contentPanel.add(btnClearLog);
		btnClearLog.setBounds(450, 570, 28, 28);

		saveFileDialog = new EducryptFileChooser(btnSaveLog);
		saveFileDialog.setTxtMode();
	}

	@Override
	public void open() {
		this.setSize(500, (int) this.getOwner().getSize().getHeight());
		this.setPreferredSize(this.getSize());
		// Set location relative to parent center
		Window owner = this.getOwner();
		this.setLocation(owner.getX() + owner.getWidth(), owner.getY());
		this.setVisible(true);
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnClearLog.setActionCommand(CLEAR_LOG);
		btnClearLog.addActionListener(ctr);

		btnSaveLog.setActionCommand(SAVE_LOG);
		btnSaveLog.addActionListener(ctr);

		saveFileDialog.addListener(ctr);
	}

	@Override
	public void clearLog() {
		try {
			this.txtLog.getStyledDocument().remove(0,
					txtLog.getStyledDocument().getLength());
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void addLogMessage(int type, String text) {
		SimpleAttributeSet attrs = GuiUtil.formatConsoleMessage(type);
		try {
			txtLog.getStyledDocument().insertString(
					txtLog.getStyledDocument().getLength(), text + "\n", attrs);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public EducryptFileChooserView getSaveFileView() {
		return saveFileDialog;
	}

	@Override
	public String getLog() {
		return txtLog.getText();
	}
}
